//
//  GolfDataModel.swift
//  TimeBoat
//
//  Created by Jun on 2022/9/8.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import Foundation

/// 高尔夫
@objcMembers class GolfDataModel: NSObject {
    /// 总杆数 4Byte
    var rodsNumber: UInt32 = 0

    /// 所用时间 4Byte
    var totalTime: UInt32 = 0

    /// 总计步数 4Byte
    var totalStep: UInt32 = 0

    /// 起始点经度 4Byte 单位：0.00001
    var startLng: UInt32 = 0

    /// 起始点纬度 4Byte 单位：0.00001
    var startLat: UInt32 = 0

    /// 经度数据数组 int8[]
    var lngArray: [UInt8]? = nil

    /// 纬度数据数组 int8[]
    var latArray: [UInt8]? = nil

    /// 速度数组 uint8[] 0.1km/h
    var speedArray: [UInt8]? = nil

    /// 海拔高度数组 int16[]
    var altitudeList: [UInt16]? = nil

    /// 心率测量数组 uint8[] (单位:次/分钟，每分钟记录一次数据)
    var heartRateList: [UInt8]? = nil

    /// 每洞最长击球距离 uint16[]
    var ballDistanceList: [UInt16]? = nil

    /// 每洞杆数 uint8[]
    var holeCount: [UInt8]? = nil
    
    convenience init(buf: [UInt8]) {
        self.init()
        
        var index: Int = 0
        
        if (index + 4 <= buf.count) {
            // 总杆数 4Byte
            rodsNumber = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            // 所用时间 4Byte
            totalTime = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            // 总计步数 4Byte
            totalStep = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            // 起始点经度 4Byte 单位：0.00001
            startLng = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            // 起始点纬度
            startLat = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

         if (index + 2 <= buf.count) {
             // 经度数据数组,经度增量，精度 0.00001 度,每 5 秒钟记录一次数据
             let lngArrayLength = Int(UInt16.toUInt16(buf: buf, offset: index))
             index += 2

             if (lngArrayLength > 0) {
                 if (index + lngArrayLength <= buf.count) {
                     lngArray = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: lngArrayLength)
                     index += lngArrayLength
                 } else {
                     Log.debug("index + lngArrayLength is overflow, \(index), \(lngArrayLength)")
                 }
             }
         }

         if (index + 2 <= buf.count) {
             //纬度数据数组 纬度增量，精度 0.00001 度,每 5 秒钟记录一次数据
             let latArrayLength = Int(UInt16.toUInt16(buf: buf, offset: index))
             index += 2

             if (latArrayLength > 0) {
                 if (index + latArrayLength <= buf.count) {
                     latArray = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: latArrayLength)
                     index += latArrayLength
                 } else {
                     Log.debug("index + latArrayLength is overflow, \(index), \(latArrayLength)")
                 }
             }
         }

         if (index + 2 <= buf.count) {
             // 速度数组
             let speedLength = Int(UInt16.toUInt16(buf: buf, offset: index))
             index += 2

             if (speedLength > 0) {
                 if (index + speedLength <= buf.count) {
                     speedArray = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: speedLength)
                     index += speedLength
                 } else {
                     Log.debug("index + speedLength is overflow, \(index), \(speedLength)")
                 }
             }
         }

         if (index + 2 <= buf.count) {
             // 海拔高度数组 单位:秒, 每公里所用时间，每公里记录一次数据
             let altitudeLength = Int(UInt16.toUInt16(buf: buf, offset: index))
             index += 2

             if (altitudeLength > 0) {
                 if (index + altitudeLength <= buf.count) {
                     altitudeList = [UInt16]()
                     for _ in 0...(altitudeLength-1) {
                         let data = UInt16.toUInt16(buf: buf, offset: index)
                         index += 2
                         altitudeList?.append(data)
                     }
                 } else {
                     Log.debug("index + altitudeLength is overflow, \(index), \(altitudeLength)")
                 }
             }
         }

         if (index + 2 <= buf.count) {
             // 心率测量数组
             let heartRateLength = Int(UInt16.toUInt16(buf: buf, offset: index))
             index += 2

             if (heartRateLength > 0) {
                 if (index + heartRateLength <= buf.count) {
                     heartRateList = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: heartRateLength)
                     index += heartRateLength
                 } else {
                     Log.debug("index + heartRateLength is overflow, \(index), \(heartRateLength)")
                 }
             }
         }

         if (index + 2 <= buf.count) {
             // 每洞最长击球距离 uint16[]
             let ballDistanceLength = Int(UInt16.toUInt16(buf: buf, offset: index))
             index += 2

             if (ballDistanceLength > 0) {
                 if (index + ballDistanceLength * 2 <= buf.count) {
                     ballDistanceList = [UInt16]()
                     for _ in 0...(ballDistanceLength-1) {
                         let data = UInt16.toUInt16(buf: buf, offset: index)
                         index += 2
                         ballDistanceList?.append(data)
                     }
                 } else {
                     Log.debug("index + ballDistanceLength is overflow, \(index), \(ballDistanceLength)")
                 }
             }
         }

         if (index + 2 <= buf.count) {
             // 每洞杆数 uint8[]
             let holeLength = Int(UInt16.toUInt16(buf: buf, offset: index))
             index += 2

             if (holeLength > 0) {
                 if (index + holeLength <= buf.count) {
                     holeCount = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: holeLength)
                 } else {
                     Log.debug("index + holeLength is overflow, \(index), \(holeLength)")
                 }
             }
         }
    }
    
    @objc func toString() -> String {
        var str = self.yy_modelToJSONString()
        if nil == str {
            str = ""
        }
        
        return str!
    }
}
